package gui;

public class NiceScale {

	private static double maxTicks = 10;
	private static double tickSpacing;
	private static double range;
	private static double niceMin;
	private static double niceMax;

	public static void calculate(double minPoint, double maxPoint) {
		if(minPoint > maxPoint) {
			double temp = minPoint;
			minPoint = maxPoint;
			maxPoint = temp;
		}
		range = niceNum(maxPoint - minPoint, false);
		tickSpacing = niceNum(range / (maxTicks - 1), true);
		niceMin = Math.floor(minPoint / tickSpacing) * tickSpacing;
		niceMax = Math.ceil(maxPoint / tickSpacing) * tickSpacing;
	}

	private static double niceNum(double range, boolean round) {
		double exponent;
		double fraction;
		double niceFraction;
		exponent = Math.floor(Math.log10(range));
		fraction = range / Math.pow(10, exponent);
		if (round) {
			if (fraction < 1.5)
				niceFraction = 1;
			else if (fraction < 3)
				niceFraction = 2;
			else if (fraction < 7)
				niceFraction = 5;
			else
				niceFraction = 10;
		} else {
			if (fraction <= 1)
				niceFraction = 1;
			else if (fraction <= 2)
				niceFraction = 2;
			else if (fraction <= 5)
				niceFraction = 5;
			else
				niceFraction = 10;
		}
		return niceFraction * Math.pow(10, exponent);
	}

	public static double spacing() {
		return tickSpacing;
	}

	public static double min() {
		return niceMin;
	}

	public static double max() {
		return niceMax;
	}

	public static void main(String[] args) {
		NiceScale.calculate(-1, 200);
		System.out.println(NiceScale.spacing() + " " + NiceScale.min() + " " + NiceScale.max());
	}
}